/**
 * 部门管理初始化
 */
var Recharge = {
    id: "RechargeTable",	//表格id
    seItem: null,		//选中的条目
    table: null,
    layerIndex: -1
};

/**
 * 初始化表格的列
 */
Recharge.initColumn = function () {
    var columns = [
        {field: 'selectItem', radio: true},

        {title: 'id', field: 'id', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {title: 'customerId', field: 'customerId', visible: false, align: 'center', valign: 'middle', width: '50px'},
        {
            title: 'reservationsId',
            field: 'reservationsId',
            visible: false,
            align: 'center',
            valign: 'middle',
            width: '50px'
        },
        {title: '客户姓名', field: 'customerName', align: 'center', valign: 'middle', sortable: true,width:120,},
        {title: '客户电话', field: 'customerPhone', align: 'center', valign: 'middle', sortable: true,width:120,},
        {
            title: '客户性别', field: 'customerSex', align: 'center', valign: 'middle', sortable: true,width:120,
            formatter: function (value, row, index) {
                if (value == 1) {
                    return "男";
                } else if (value == 2) {
                    return "女"
                } else {
                    return "未知"
                }
            }
        },
        {title: '客户年龄', field: 'customerAge', align: 'center', valign: 'middle', sortable: true,width:120,},
        {title: '客户状态', field: 'customerStatus', align: 'center', valign: 'middle', sortable: true,width:120,},
        {title: '折后总金额', field: 'foldingPay', align: 'center', valign: 'middle', sortable: true,width:120,},
        {
            title: '已收金额', field: 'getMoney', align: 'center', valign: 'middle', sortable: true,width:120,
            formatter: function (value, row, index) {
                if (value == "" || value == null) {
                    return 0;
                } else {
                    return value;
                }
            }
        },
        {title: '本次实收金额', field: 'serviceActualPay', align: 'center', valign: 'middle', sortable: true,width:120},
        {
            title: '欠款',
            field: 'accountArrears',
            align: 'center',
            valign: 'middle',
            sortable: true,
            width:120,
            formatter: function (value, row, index) {
                if (row.dealStatus == "已开单") {
                    return "";
                } else {
                    return value;
                }
            }
        },
        {title: '状态', field: 'dealStatus', align: 'center', valign: 'middle', sortable: true,width:120,},
        {title: '单据号', field: 'documentNumber', align: 'center', valign: 'middle', sortable: true,width:120,},
        {title: 'tc姓名', field: 'tcName', align: 'center', valign: 'middle', sortable: true,width:120,},
        {title: '一级渠道', field: 'oneChannel', align: 'center', valign: 'middle', sortable: true,width:120,},
        {title: '二级渠道', field: 'twoChannel', align: 'center', valign: 'middle', sortable: true,width:120,},
        {title: '三级渠道', field: 'thrChannel', align: 'center', valign: 'middle', sortable: true,width:120,},
        {
            title: '操作时间', field: 'operationTime', align: 'center', valign: 'middle', sortable: true,width:120,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },

        {
            title: '时间', field: 'getTime', align: 'center', valign: 'middle', sortable: true,width:120,
            formatter: function (value, row, index) {
                return value.substring(0, 19);
            }
        },
        {title: '备注', field: 'remarks', align: 'center', valign: 'middle', sortable: true,width:120},
        {title: '备注2', field: 'remark', visible: false, align: 'center', valign: 'middle',width:120},
    ]
    return columns;
};

Recharge.close = function () {
    // parent.layer.close(window.parent.Customer.layerIndex);
    var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
    parent.layer.close(index);
}

Recharge.closes = function () {
    var customerId = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var operationTime = $("#operationTime").val();
    $.ajax({
        url: Feng.ctxPath + "/recharge/closesBalance",
        type: "post",
        data: {
            "customerId": customerId,
            "reservationsId": reservationsId,
            "operationTime": operationTime
        },
        success: function (data) {
            var index = parent.layer.getFrameIndex(window.name); //获取窗口索引
            parent.layer.close(index);
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
}
/**
 * 检查是否选中
 */
Recharge.check = function () {
    var selected = $('#' + this.id).bootstrapTable('getSelections');
    if (selected.length == 0) {
        Feng.info("请先选中表格中的某一记录！");
        return false;
    } else {
        Recharge.seItem = selected[0];
        return true;
    }
};

Recharge.delete = function () {
    if (this.check()) {
        if (this.seItem.dealStatus == "已开单") {
            var r = confirm("是否删除此单？")
            if (r == true) {
                var ajax = new $ax(Feng.ctxPath + "/recharge/delete/" + this.seItem.id, function (data) {
                    Feng.success("删除成功!");
                    Recharge.table.refresh();
                }, function (data) {
                    Feng.error("删除失败!" + data.responseJSON.message + "!");
                });
                ajax.start();
            } else {
            }
        } else {
            alert("只能删除已开单项目");
            return false;
        }
    }
};


Recharge.openSupplement = function () {
    if (this.check()) {
        if (Recharge.seItem.dealStatus == "已开单") {
            alert("请选择结账按钮！");
            return;
        }
        if (Recharge.seItem.dealStatus == "特殊补欠款") {
            alert("请选择结补签款按钮！");
            return;
        }
        var arrears = Recharge.seItem.accountArrears;
        var id = Recharge.seItem.id;
        var ai = arrears + "," + id;
        var index = layer.open({
            type: 2,
            title: '付款',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/consultant/to_supplement/' + ai
        });
        this.layerIndex = index;
    }
}

Recharge.openChoiceCheckoutMethod = function (id) {
    var reservationsId = $('#reservationsId').val();
    var customerId = $('#customerId').val();
    var operationTime = $('#operationTime').val();
    var id2 = $('#id2').val();
    var getTime = $('#getTime').val();
    var payorrefund = $('#payorrefund').val();
    var croi = getTime + "," + id + "," + payorrefund + "," + customerId + ","
        + reservationsId + "," + id2;
    if (payorrefund == '结账') {
        var index = layer.open({
            type: 2,
            title: "输入结账金额",
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/recharge/getCheckoutMethod/' + croi
        });
    } else if (payorrefund == '退款') {
        var index = layer.open({
            type: 2,
            title: "输入退款金额",
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/recharge/getCheckoutMethod/' + croi
        });
    }
    this.layerIndex = index;
}

Recharge.choiceCheckoutMethod = function (id) {
    var reservationsId = $('#reservationsId').val();
    var customerId = $('#customerId').val();
    var operationTime = $('#operationTime').val();
    var totleFoldingPay = $('#totleFoldingPay').val();
    var getTime = $("#getTime").val();
    var ysMoney = document.getElementById('ysMoney').innerHTML;   //应收金额
    var payorrefund = "";

    if ((ysMoney.substring(0, 1)) == '-') {
        payorrefund = "退款";
        var rco = String(id) + "," + payorrefund + "," + reservationsId + "," + customerId + ","
            + operationTime + "," + getTime;
        var index = layer.open({
            type: 2,
            title: '选择退款方式',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/recharge/to_choiceCheckoutMethod/' + rco
        });
    } else {
        payorrefund = "结账";
        var rco = id + "," + payorrefund + "," + reservationsId + "," + customerId  + ","
            + operationTime + "," + getTime;
        var index = layer.open({
            type: 2,
            title: '选择结账方式',
            area: ['100%', '100%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/recharge/to_choiceCheckoutMethod/' + rco
        });
    }

    this.layerIndex = index;
}

Recharge.choiceCheckoutMethodd = function () {
    var reservationsId = $('#reservationsId').val();
    var customerId = $('#customerId').val();
    var operationTime = $('#operationTime').val();
    var rco = reservationsId + "," + customerId + "," + operationTime;
    var index = layer.open({
        type: 2,
        title: '选择结账方式',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/recharge/to_choiceCheckoutMethodd/' + rco
    });
    this.layerIndex = index;
}

var addBalanceCount = 0;
Recharge.addBalance = function () {
    addBalanceCount++;
    if (addBalanceCount == 1) {
        var customerId = $("#customerId").val();
        var reservationsId = $("#reservationsId").val();
        var getTime = $("#getTime").val();
        var totleFoldingPay = $("#totleFoldingPay").val();
        var totleServiceActualPay = $("#totleServiceActualPay").val();
        var checkoutMethod = $("#checkoutMethod").val();
        var surplusAmount = window.parent.document.getElementById("surplusAmount").value;
        var total = window.parent.document.getElementById("total").value;
        var serviceActualPay = $("#serviceActualPay").val();
        var id2 = $("#id2").val();
        var payorrefund = document.getElementById('payorrefund').value;   //退款还是结账

        var str = [];
        var endtr = "";
        var amountOfBalance = 0;
        for(var i=0 ; i<$(".checkoutMethodName").length ; i++){
            if ($(".amountOfBalance2").eq(i).val() == "" || $(".amountOfBalance2").eq(i).val() == null) {
                alert("请输入结账金额！");
                addBalanceCount = 0;
                return;
            }else if (payorrefund == '结账') {
                endtr = $(".remarks").eq(i).val() + "#" + $(".checkoutMethodName").eq(i).val() + "#" + $(".amountOfBalance2").eq(i).val() ;
                amountOfBalance += parseFloat($(".amountOfBalance2").eq(i).val());
                str.push(endtr)
            }else if (payorrefund == '退款') {
                endtr = $(".remarks").eq(i).val() + "#" + $(".checkoutMethodName").eq(i).val() + "#-" + $(".amountOfBalance2").eq(i).val() ;
                amountOfBalance += parseFloat($(".amountOfBalance2").eq(i).val());
                str.push(endtr)
            }
        }

        /*if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(amountOfBalance))) {
            alert("请输入正确金额！");
            return;
        }  */

        if (payorrefund == '退款') {
            serviceActualPay = serviceActualPay.substring(1);
            if (parseFloat(amountOfBalance) > parseFloat(serviceActualPay)) {
                alert("结账金额不应大于折后金额！");
                addBalanceCount = 0;
                return;
            }
            amountOfBalance = "-" + amountOfBalance;
        }

        if (parseFloat(amountOfBalance) > parseFloat(serviceActualPay)) {
            alert("结账金额不应大于折后金额！");
            addBalanceCount = 0;
            return;
        }

        $.ajax({
            url: Feng.ctxPath + "/recharge/addBalance",
            type: "post",
            traditional:true,
            data: {
                "customerId": customerId,
                "reservationsId": reservationsId,
                "id2": id2,
                "getTime": getTime,
                "str": str
            },
            success: function (data1) {
                window.parent.document.getElementById("surplusAmount").value = parseFloat(surplusAmount*100)/100 + parseFloat(amountOfBalance*100)/100;
                window.parent.document.getElementById("total").value = parseFloat(amountOfBalance*100)/100 + parseFloat(total*100)/100;

                var checkHtml = "";
                for (var i = 0; i < data1.length; i++) {
                    checkHtml += '<div class="row">&nbsp&nbsp结账方式：' +  data1[i].checkoutMethod + '&nbsp&nbsp结账金额：' +  data1[i].actualAccountBalance + '&nbsp&nbsp备注：' +  data1[i].remarks + '</div>';
                    // window.parent.document.getElementById("checkoutMethodDetails").innerHTML +=
                    //     ('<div class="row">&nbsp&nbsp结账方式：' +  data1[i].checkoutMethod + '&nbsp&nbsp结账金额：' +  data1[i].actualAccountBalance + '</div>' +
                    //         '<div class="row">&nbsp&nbsp备注：' +  data1[i].remarks + '</div>')
                }
                window.parent.document.getElementById("checkoutMethodDetails").innerHTML = checkHtml;
                Feng.success("添加结账成功！");
                $('#cancel', document).click();
            },
            error: function (data) {
                alert('内部故障!');
            }
        });
    }
}

Recharge.addBalanced = function () {
    var customerId = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var operationTime = $("#operationTime").val();
    var amountOfBalance = $("#amountOfBalance").val();
    var totleServiceActualPay = $("#totleServiceActualPay").val();
    var checkoutMethod = $("#checkoutMethod").val();
    var surplusAmount = window.parent.parent.document.getElementById("surplusAmount").value;
    var total = window.parent.parent.document.getElementById("total").value;
    var serviceActualPay = $("#serviceActualPay").val();
    var remarks = $("#remarks").val();
    if (amountOfBalance == null || amountOfBalance == "") {
        alert("请输入结账金额！");
        return;
    }
    if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(amountOfBalance))) {
        alert("请输入正确金额！");
        return;
    }
    if (parseFloat(amountOfBalance) > parseFloat(serviceActualPay)) {
        alert("结账金额总额不应大于折后总金额！！！");
        return;
    }
    $.ajax({
        url: Feng.ctxPath + "/recharge/addBalance",
        type: "post",
        data: {
            "customerId": customerId,
            "reservationsId": reservationsId,
            "remarks": remarks,
            "operationTime": operationTime,
            "checkoutMethod": checkoutMethod,
            "amountOfBalance": amountOfBalance
        },
        success: function (data) {
            window.parent.parent.document.getElementById("surplusAmount").value = parseFloat(surplusAmount) + parseFloat(amountOfBalance);
            window.parent.parent.document.getElementById("total").value = parseFloat(amountOfBalance) + parseFloat(total);
            window.parent.parent.document.getElementById("checkoutMethodDetails").innerHTML +=
                ('<div class="row">&nbsp&nbsp结账方式：' + checkoutMethod + '&nbsp&nbsp结账金额：' + amountOfBalance + '</div>' +
                    '<div class="row">&nbsp&nbsp备注：' + remarks + '</div>')
            Feng.success("添加结账成功！");
            $('#cancel', parent.document).click();
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
}

Recharge.addBalances = function () {
    var customerId = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var operationTime = $("#operationTime").val();
    var amountOfBalance = $("#amountOfBalance").val();
    var accountArrears = $("#accountArrears").val();
    var checkoutMethod = $("#checkoutMethod").val();
    var remarks = $("#remarks").val();
    var serviceActualPay = $("#serviceActualPay").val();
    if (amountOfBalance == null || amountOfBalance == "") {
        alert("请输入结账金额！");
        return;
    }
    if (!(/^[0-9]{1}\d{0,6}(\.\d{1,2})?$/.test(amountOfBalance))) {
        alert("请输入正确金额！");
        return;
    }
    console.log(amountOfBalance);
    console.log(accountArrears);
    if (parseFloat(amountOfBalance) > parseFloat(accountArrears)) {
        alert("结账金额总额不应大于折后总金额！！");
        return;
    }
    $.ajax({
        url: Feng.ctxPath + "/recharge/addBalances",
        type: "post",
        data: {
            "customerId": customerId,
            "reservationsId": reservationsId,
            "remarks": remarks,
            "operationTime": operationTime,
            "checkoutMethod": checkoutMethod,
            "amountOfBalance": amountOfBalance
        },
        success: function (data) {
            Feng.success("补签款成功！");
            window.parent.parent.parent.Recharge.table.refresh();
            $('#cancel', parent.parent.document).click();
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
}

var getCount = 0;
Recharge.checkOut = function () {
    getCount++;
    if (getCount == 1) {
        var customerId = $("#customerId").val();
        var reservationsId = $("#reservationsId").val();
        var operationTime = $("#operationTime").val();
        var amountOfBalance = $("#amountOfBalance").val();
        var totleFoldingPay = $("#totleFoldingPay").val();
        var totleServiceActualPay = $("#totleServiceActualPay").val();
        var checkoutMethod = $("#checkoutMethod").val();
        var remarks = $("#remarks").val();
        var total = $("#total").val();
        var getTime = $("#getTime").val();
        console.log(total);
        console.log(totleServiceActualPay);
        if (parseFloat(total) < parseFloat(totleServiceActualPay)) {
            alert("结账未结束！请继续");
            getCount = 0;
            return;
        }
        $.ajax({
            url: Feng.ctxPath + "/recharge/checkOut",
            type: "post",
            data: {
                "customerId": customerId,
                "reservationsId": reservationsId,
                "operationTime": operationTime,
                "getTime": getTime
            },
            success: function (data) {
                Feng.success("结账成功！");
                window.parent.Recharge.table.refresh();
                Recharge.close();
                Recharge.total();
            },
            error: function (data) {
                alert('内部故障!');
            }
        });
    }
}

Recharge.checkOutd = function () {
    var customerId = $("#customerId").val();
    var reservationsId = $("#reservationsId").val();
    var operationTime = $("#operationTime").val();
    var amountOfBalance = $("#amountOfBalance").val();
    var totleFoldingPay = $("#totleFoldingPay").val();
    var totleServiceActualPay = $("#totleServiceActualPay").val();
    var checkoutMethod = $("#checkoutMethod").val();
    var remarks = $("#remarks").val();
    var total = $("#total").val();
    if (parseFloat(total) < parseFloat(totleServiceActualPay)) {
        alert("结账未结束！请继续");
        return;
    }
    $.ajax({
        url: Feng.ctxPath + "/recharge/checkOutd",
        type: "post",
        data: {
            "customerId": customerId,
            "reservationsId": reservationsId,
            "operationTime": operationTime
        },
        success: function (data) {
            Feng.success("结账成功！");
            window.parent.Recharge.table.refresh();
            Recharge.close();
            Recharge.total();
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
}

Recharge.specialRecharge = function () {
    if (this.check()) {
        if (Recharge.seItem.dealStatus == "已开单") {
            alert("请选择结账按钮！");
            return;
        }
        if (Recharge.seItem.dealStatus == "已成交") {
            alert("请选择特补欠款按钮！");
            return;
        }
        var index = layer.open({
            type: 2,
            title: '结账',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/recharge/to_specialRecharge/' + Recharge.seItem.id
        });
        this.layerIndex = index;
    }
}

/**
 * 打印
 */
Recharge.print = function () {
    if (this.check()) {
        if (Recharge.seItem.dealStatus != "已成交") {
            alert("该单据没有成交，不能打印！");
            return;
        }
        var index = layer.open({
            type: 2,
            title: '打印',
            area: ['50%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/consultant/to_print/' + Recharge.seItem.id // 传入选中行的客户ID
        });
        this.layerIndex = index;
    }
}

/*
导入费用明细
 */
Recharge.uploadDealInfo = function () {
    var index = layer.open({
        type: 2,
        title: '批量上传',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/recharge/dealInfo_import/'
    });
    this.layerIndex = index;
};

/*
导入用户欠费明细
 */
Recharge.uploadArrears = function () {
    var index = layer.open({
        type: 2,
        title: '批量上传',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/recharge/arrearInfo_import/'
    });
    this.layerIndex = index;
};

/*
导入病历号
 */
Recharge.uploadMedicalNumber = function () {
    var index = layer.open({
        type: 2,
        title: '批量上传',
        area: ['90%', '90%'], //宽高
        fix: true, //不固定
        maxmin: true,
        content: Feng.ctxPath + '/recharge/medicalNumber_import/'
    });
    this.layerIndex = index;
};


Recharge.recharge = function () {
    if (this.check()) {
        if (Recharge.seItem.dealStatus == "特殊补欠款") {
            alert("请选择结补签款按钮！");
            return;
        }
        if (Recharge.seItem.dealStatus == "已成交") {
            alert("请选择特补欠款按钮！");
            return;
        }
        var index = layer.open({
            type: 2,
            title: '结账',
            area: ['60%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/recharge/to_checkoutMethod/' + Recharge.seItem.id
        });
        this.layerIndex = index;
    }


    // if (this.check()) {
    //     if(Recharge.seItem.dealStatus=="已成交"){
    //         alert("已结账！");
    //         return;
    //     }
    //     if(confirm('确定要执行此操作吗?'))
    //     {
    //         $.ajax({
    //             url: Feng.ctxPath + "/recharge/addPayments",
    //             type: "post",
    //             data: {
    //                 "id": Recharge.seItem.id
    //             },
    //             success: function (data) {
    //                 Recharge.table.refresh({query: Recharge.formParams()});
    //             },
    //             error: function (data) {
    //                 alert('内部故障!');
    //             }
    //         });
    //         return true;
    //     }
    //     return false;
    //     // var index = layer.open({
    //     //     type: 2,
    //     //     title: '充值',
    //     //     area: ['1400px', '600px'], //宽高
    //     //     fix: true, //不固定
    //     //     maxmin: true,
    //     //     content: Feng.ctxPath + '/customer/rechargeind/' + Customer.seItem.id
    //     // });
    //     // this.layerIndex = index;
    // }
};

/**
 * 统计总人数
 */
// Consultant.total = function () {
//     var param1 = $("#startTime").val();  //到诊开始时间
//     var param2 = $("#endTime").val();   //到诊结束时间
//     var param3 = $("#oneChannel").val();  // 一级渠道
//     var param4 = $("#twoChannel").val(); //二级渠道
//     var param5 = $("#thrChannel").val(); //三级渠道
//     var param6 = $("#dealStatus").val(); //成交状态
//     $.ajax({
//         type: "GET",
//         url: Feng.ctxPath + "",
//         data: {
//             param1: param1, param2: param2, param3: param3,param4: param4,param5:param5,param6:param6
//         },
//         dataType: "json",
//         success: function (data) {
//             $("#total").html();//总人数
//             $("#totalMoney").html();//总金额
//             $("#first").html();//新客首次
//             $("#firstMoney").html();//新客首次金额
//             $("#secondary").html();//新客二次
//             $("#secondaryMoney").html();//新客二次金额
//             $("#oldGuest").html();//老客
//             $("#oldGuestMoney").html();//老客金额
//             $("#revisit").html();//复诊
//             $("#revisitMoney").html();//复诊金额
//         }
//     });
// }


/**
 * 查询预约客户列表
 */
Recharge.inquire = function () {
    Recharge.search()
    Recharge.total();
}

Recharge.search = function () {
    Recharge.table.refresh({query: Recharge.formParams()});
};

Recharge.resetSearch = function () {
    $("#oneChannel").val("请选择");
    $("#twoChannel").val("请选择");
    $("#thrChannel").val("请选择");
    $("#dealStatus").val("");
    $("#customerStatus").val("");
    $("#reach").val("");
    document.getElementById("startTime").value = y + "-" + m + "-" + d + " " + "16:00:00";
    document.getElementById("endTime").value = Y + "-" + M + "-" + D + " " + "16:00:00";
    Recharge.search();
};

/**
 * 查询表单提交参数对象
 * @returns {{}}
 */
Recharge.formParams = function () {
    var queryData = {};
    queryData['reach'] = $("#reach").val();
    queryData['startTime'] = $("#startTime").val();
    queryData['endTime'] = $("#endTime").val();
    queryData['dealStatus'] = $("#dealStatus").val();
    queryData['customerStatus'] = $("#customerStatus").val();
    queryData['oneChannel'] = $("#oneChannel").val();
    queryData['twoChannel'] = $("#twoChannel").val();
    queryData['thrChannel'] = $("#thrChannel").val();
    return queryData;
};

var myDate = new Date();
var Y = myDate.getFullYear();
var M = myDate.getMonth() + 1;
var D = myDate.getDate();
if(M < 10){
    M = "0" + M
}
if(D < 10){
    D = "0" + D
}
var date = new Date();
date.setDate(date.getDate()-1);
var y = date.getFullYear();
var m = date.getMonth() + 1;
var d = date.getDate();
if(m < 10){
    m = "0" + m
}
if(d < 10){
    d = "0" + d
}


$(function () {
    document.getElementById("startTime").value = y + "-" + m + "-" + d + " " + "16:00:00";
    document.getElementById("endTime").value = Y + "-" + M + "-" + D + " " + "16:00:00";
    var defaultColunms = Recharge.initColumn();
    var table = new BSTable(Recharge.id, "/recharge/rechargeList", defaultColunms);
    table.setPaginationType("server");
    table.setQueryParams(Recharge.formParams());
    Recharge.table = table.init();
    Recharge.search();
    Recharge.total();

    //采用定时器监听 时间内容是否变化
    var startArrivalTimeTwo = $("#startTime").val();
    var endArrivalTimeTwo = $("#endTime").val();

    var oldOneChannelValue = $("#oneChannel").val();
    var oldTwoChannelValue = $("#twoChannel").val();
    var oldThrChannelValue = $("#thrChannel").val();

    setInterval(
        function () {//定时执行方法内容
            if ($("#startTime").val() != startArrivalTimeTwo || $("#endTime").val() != endArrivalTimeTwo) {//如果时间发生变化，就触发查询事件
                Recharge.search()
                Recharge.total();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                startArrivalTimeTwo = $("#startTime").val();
                endArrivalTimeTwo = $("#endTime").val();
            }
            if ($("#oneChannel").val() != oldOneChannelValue || $("#oneChannel").val() != oldOneChannelValue) {
                Recharge.search()
                Recharge.total();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldOneChannelValue = $("#oneChannel").val();
            }
            if ($("#twoChannel").val() != oldTwoChannelValue || $("#twoChannel").val() != oldTwoChannelValue) {
                Recharge.search()
                Recharge.total();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldTwoChannelValue = $("#twoChannel").val();
            }
            if ($("#thrChannel").val() != oldThrChannelValue || $("#thrChannel").val() != oldThrChannelValue) {
                Recharge.search()
                Recharge.total();
                //如果值发生变化之后，把最新的值赋值到老的变量里面
                oldThrChannelValue = $("#thrChannel").val();
            }
        }, 500);
});

//总人数数据详情
Recharge.totalNumber = function () {
    document.getElementById("customerStatus").value = "";
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();//到诊开始时间
    queryData['endTime'] = $("#endTime").val();//到诊结束时间
    queryData['oneChannel'] = $("#oneChannel").val();// 一级渠道
    queryData['twoChannel'] = $("#twoChannel").val();//二级渠道
    queryData['thrChannel'] = $("#thrChannel").val();//三级渠道
    queryData['dealStatus'] = $("#dealStatus").val();//成交状态
    queryData['customerStatus'] = ""; //加入客户状态，总人数
    Recharge.table.refresh({query: queryData});
};
//新客首次数据详情
Recharge.newCustomsData = function () {
    document.getElementById("customerStatus").value = "新客首次";
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();//到诊开始时间
    queryData['endTime'] = $("#endTime").val();//到诊结束时间
    queryData['oneChannel'] = $("#oneChannel").val();// 一级渠道
    queryData['twoChannel'] = $("#twoChannel").val();//二级渠道
    queryData['thrChannel'] = $("#thrChannel").val();//三级渠道
    queryData['dealStatus'] = $("#dealStatus").val();//成交状态
    queryData['customerStatus'] = "新客首次";//加入客户状态，新客首次
    Recharge.table.refresh({query: queryData});
};
//新客二次数据详情
Recharge.newSecondary = function () {
    document.getElementById("customerStatus").value = "新客二次";
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();//到诊开始时间
    queryData['endTime'] = $("#endTime").val();//到诊结束时间
    queryData['oneChannel'] = $("#oneChannel").val();// 一级渠道
    queryData['twoChannel'] = $("#twoChannel").val();//二级渠道
    queryData['thrChannel'] = $("#thrChannel").val();//三级渠道
    queryData['dealStatus'] = $("#dealStatus").val();//成交状态
    queryData['customerStatus'] = "新客二次";//加入客户状态，新客二次
    Recharge.table.refresh({query: queryData});
};
//老客数据详情
Recharge.newOldGuest = function () {
    document.getElementById("customerStatus").value = "老客";
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();//到诊开始时间
    queryData['endTime'] = $("#endTime").val();//到诊结束时间
    queryData['oneChannel'] = $("#oneChannel").val();// 一级渠道
    queryData['twoChannel'] = $("#twoChannel").val();//二级渠道
    queryData['thrChannel'] = $("#thrChannel").val();//三级渠道
    queryData['dealStatus'] = $("#dealStatus").val();//成交状态
    queryData['customerStatus'] = "老客";//加入客户状态，老客
    Recharge.table.refresh({query: queryData});
};
//复诊数据详情
Recharge.newRevisit = function () {
    document.getElementById("customerStatus").value = "复诊";
    var queryData = {};
    queryData['startTime'] = $("#startTime").val();//到诊开始时间
    queryData['endTime'] = $("#endTime").val();//到诊结束时间
    queryData['oneChannel'] = $("#oneChannel").val();// 一级渠道
    queryData['twoChannel'] = $("#twoChannel").val();//二级渠道
    queryData['thrChannel'] = $("#thrChannel").val();//三级渠道
    queryData['dealStatus'] = $("#dealStatus").val();//成交状态
    queryData['customerStatus'] = "复诊";//加入客户状态，复诊
    Recharge.table.refresh({query: queryData});
};

/**
 * 统计总人数
 */
Recharge.total = function () {
    var startArrivalTimeTwo = $("#startTime").val();
    var reach = $("#reach").val();
    var endArrivalTimeTwo = $("#endTime").val();
    var dealStatus = $("#dealStatus").val();
    var oldOneChannelValue = $("#oneChannel").val();
    var oldTwoChannelValue = $("#twoChannel").val();
    var oldThrChannelValue = $("#thrChannel").val();

    $.ajax({
        type: "GET",
        url: Feng.ctxPath + "/recharge/rechargeTotal",
        data: {
            startArrivalTimeTwo: startArrivalTimeTwo,
            endArrivalTimeTwo: endArrivalTimeTwo,
            dealStatus: dealStatus,
            reach: reach,
            oldOneChannelValue: oldOneChannelValue,
            oldTwoChannelValue: oldTwoChannelValue,
            oldThrChannelValue: oldThrChannelValue
        },
        dataType: "json",
        success: function (data) {
            $("#total").html(data[0].countNum);//总人数
            $("#first").html(data[1].countNum);//新客首次
            $("#secondary").html(data[2].countNum);//新客二次
            $("#oldGuest").html(data[3].countNum);//老客
            $("#revisit").html(data[4].countNum);//复诊

            $("#totalMoney").html(data[0].toalMoney);//总人数
            $("#firstMoney").html(data[1].toalMoney);//新客首次
            $("#secondaryMoney").html(data[2].toalMoney);//新客二次
            $("#oldGuestMoney").html(data[3].toalMoney);//老客
            $("#revisitMoney").html(data[4].toalMoney);//复诊
        }
    });
};

//修改时间
Recharge.updateTime = function () {
    if (this.check()) {
        var customerId = Recharge.seItem.customerId;
        var time = Recharge.seItem.getTime;
        var ids = customerId +","+ time
        if (Recharge.seItem.dealStatus != "已成交") {
            alert("该单据没有成交，不能修改！");
            return;
        }
        var index = layer.open({
            type: 2,
            title: '修改时间',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/recharge/update_time/' + ids
        });
        this.layerIndex = index;
    }
}

//修改时间
Recharge.updateDocumentNumber = function () {
    if (this.check()) {
        var customerId = Recharge.seItem.customerId;
        var time = Recharge.seItem.getTime;
        var ids = customerId +","+ time
        if (Recharge.seItem.dealStatus != "已成交") {
            alert("该单据没有成交，不能修改！");
            return;
        }
        var index = layer.open({
            type: 2,
            title: '修改时间',
            area: ['90%', '90%'], //宽高
            fix: true, //不固定
            maxmin: true,
            content: Feng.ctxPath + '/recharge/update_documentNumber/' + ids
        });
        this.layerIndex = index;
    }
}

Recharge.updateTimes = function () {
    var updateTime = $("#updateTime").val();
    var getTime = $("#getTime").val();
    var customerId = $("#customerId").val();
    $.ajax({
        url: Feng.ctxPath + "/recharge/updateGetTime",
        type: "post",
        data: {
            "customerId": customerId,
            "updateTime": updateTime,
            "getTime": getTime
        },
        success: function (data) {
            Feng.success(" 修改结账时间成功！");
            window.parent.Recharge.table.refresh();
            Recharge.close();
        },
        error: function (data) {
            alert('内部故障!');
        }
    });
}